<?php


namespace app\controller;
use app\model\Article as ArticleModel;
use think\exception\ErrorException;
use think\facade\Db;
use think\facade\Validate;
use think\Request;
use app\model\Section as SectionModel;

class Section extends Base
{
    public function create(Request $request){
        $data=$request->param();
        if($data['section']!=null){
            $section=SectionModel::create([
                'sName'=>$data['section']
            ]);
            $section=Db::name('section')->where('sId',$section->id)->find();
            if($section){
                return $this->createApi($section,'创建模块成功',200);
            }
        }else{
            return $this->createApi([],'创建模块失败',204);
        }
    }
    public function setHot(Request $request){
        $data=$request->param();
        if($data['section']!=null){
            $section=SectionModel::where('sId',$data['section'])->find();
            $section->sClickNum++;
            $section->save();
            if($section){
                return $this->createApi($section,'热度更新成功',200);
            }else{
                return $this->createApi([],'热度更新失败',204);
            }
        }
    }
    public function delete($id){
        if(!Validate::isInteger($id)){
            return $this->createApi([], '数据不合法', 400);
        }
        try{
            $sec=SectionModel::where('sId',$id)->find($id);
            $sec->delete();
            return $this->createApi([], '删除成功', 200);
        }catch(\Error $e){
            return $this->createApi([], '无法删除', 400);
        }
    }
    public function totalPage(Request $request){
        $data=$request->param();
        if($data['section']!=null){
            $count=ArticleModel::where('sectionId',$data['section'])->count();
            $total=ceil($count/$this->page_size);
            return $this->createApi($total, '总页数', 200);
        }
    }
    public function images(){
        $images=[];
        for($i=0;$i<5;$i++){
            $image[$i]="http://121.4.214.174/storage/advertisement".($i+1);
        }
        if($images){
            return $this->createApi($images,'success',200);
        }
    }
}